package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;
import ${package.ReqVoPackage}.*;
import ${package.RespVoPackage}.*;
import ${package.Entity}.*;
import ${package.BuilderPackage}.*;
import ${package.MapperPackage}.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.mybatisplus.PageHelper;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import ${package.Service}.*;
import cn.hutool.core.bean.BeanUtil;
import com.yingxin.yx.framework.commons.id.IdHelper;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */

@Api(tags = "${table.comment!}")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${serviceVariable};

<#if buildType == 'BILL'>
    @Autowired
    private ${itemServiceClass} ${itemServiceClassVariable};
</#if>

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<${RespVo}> page(@RequestBody ${QueryVo} queryVo) {
        IPage<${RespVo}> page = ${serviceVariable}.mapper().pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<${RespVo}> list(@RequestBody ${QueryVo} queryVo) {
        return ${serviceVariable}.mapper().listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public ${RespVo} get(@PathVariable("id") String id) {
<#if buildType == 'NORMAL'>
        ${RespVo} ${entityVariable} = ${serviceVariable}.mapper().getVo(id);
        return ${entityVariable};
</#if>
<#if buildType == 'BILL'>
        ${RespVo} ${billClassVariable} = ${serviceVariable}.mapper().getVo(id);
        ${billClassVariable}.setItems(${itemServiceClassVariable}.mapper().listVoBy${firstLetterUpperForeignProperty}(id));
        return ${billClassVariable};
</#if>
    }


    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid ${CreateVo} createVo) {
    <#if buildType == 'BILL'>
        ${entity} ${billClassVariable} = ${Builder}.build(createVo);
        List<${itemClass}> items = ${Builder}.buildItems(${billClassVariable}, createVo);
        ${serviceVariable}.create(${billClassVariable}, items);
        return ${billClassVariable}.getId();
    <#elseif buildType == 'ITEM'>
        ${entity} ${itemClassVariable} = ${Builder}.build(createVo);
        ${serviceVariable}.save(${itemClassVariable});
        return ${itemClassVariable}.getId();
    <#else>
        ${entity} ${entityVariable} = ${Builder}.build(createVo);
        ${serviceVariable}.create(${entityVariable});
        return ${entityVariable}.getId();
    </#if>
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid ${UpdateVo} updateVo) {
    <#if buildType == 'BILL'>
        ${entity} ${billClassVariable} = ${Builder}.build(updateVo);
        List<${itemClass}> items = ${Builder}.buildItems(${billClassVariable}, updateVo);
        ${serviceVariable}.update(${billClassVariable}, items);
        return ${billClassVariable}.getId();
    <#elseif buildType == 'ITEM'>
        ${entity} ${itemClassVariable} = ${Builder}.build(updateVo);
        ${serviceVariable}.updateById(${itemClassVariable});
        return ${itemClassVariable}.getId();
    <#else>
        ${entity} ${entityVariable} = ${Builder}.build(updateVo);
        ${serviceVariable}.update(${entityVariable});
        return ${entityVariable}.getId();
    </#if>
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
    <#if buildType == 'BILL'>
        ${entity} ${billClassVariable} = ${serviceVariable}.getById(id);
        List<${itemClass}> items = ${itemServiceClassVariable}.listBy${firstLetterUpperForeignProperty}(id);
        ${serviceVariable}.delete(${billClassVariable}, items);
    <#else>
        ${entity} ${entityVariable} = ${serviceVariable}.getById(id);
        ${serviceVariable}.delete(${entityVariable});
    </#if>
    }

}
</#if>
